<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/4 0004
 * Time: 14:14
 */

namespace app\api\controller;


use Omnipay\Omnipay;
use think\Controller;
class Pay extends Controller
{
    public function pay()
    {
        $openid = 'owpsA0T9-Rw8emHX2QO97FbgcXOA';
        $body = '测试';
        $out_trade_no = '123';
        $total_fee = 1;
        $notify_url = 'https://chuangkegongfang/public/index.php/api/pay/notify';
        //实例支付接口
        $pay = &load_wechat('Pay');
        //获取预支付id
        $result = $pay->getPrepayId($openid, $body, $out_trade_no, $total_fee, $notify_url, $trade_type = "JSAPI");
        // 处理创建结果
        if ($result === FALSE) {
            // 接口失败的处理
            return $pay->errMsg;
        } else {
            // 接口成功的处理
	$sign = $this->sign($result);
              return json_encode($sign);
        }
    }

    public function sign($prepayid)
    {
        // 实例支付接口
        $pay = &load_wechat('Pay');

        // 创建JSAPI签名参数包
        $options = $pay->createMchPay($prepayid);
	return $options;
    }

     /**
     * 支付通知处理
     * @return type
     */
    public function notify() {
        header('Content-Type:text/xml; charset=utf-8');
        $postStr = file_get_contents("php://input");
        $notifyInfo = (array) simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        if ($notifyInfo['result_code'] == 'SUCCESS' && $notifyInfo['return_code'] == 'SUCCESS') {
            # 记录支付通知信
            file_put_contents(LOG_PATH . 'pay_notify.log', var_export($notifyInfo, TRUE));
	#判断金额是否正确,修改订单状态

            # 所有操作成功，返回正常状态
            return xml(['return_code' => 'SUCCESS', 'return_msg' => 'SAVE DATA SUCCESS']);
        }
    }
}